package com.project.restaurant.thirdpart.config;

import com.github.wxpay.sdk.IWXPayDomain;
import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConfig;
import com.github.wxpay.sdk.WXPayConstants;
import com.project.restaurant.common.util.FileOptionUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Configuration
@EnableConfigurationProperties(WXPayProperties.class)
public class WXPayAutoConfiguration {

    @Bean
    public WXPay wxPay(WXPayProperties wxPayProperties) throws Exception {

        WXNativePayConfig nativePayConfig = new WXNativePayConfig();
        nativePayConfig.setAppId(wxPayProperties.getAppId())
                .setMchId(wxPayProperties.getMchId())
                .setKey(wxPayProperties.getKey());

        if(wxPayProperties.getCertDataPath()!=null){
            try {
                nativePayConfig.setCertDataPath(wxPayProperties.getCertDataPath());
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("ShopOrderAutoConfiguration里创建NativePayConfig对象的读取证书出错");
            }
        }

        WXPayConfig config = nativePayConfig;
        WXPay wxpay = new WXPay(config);

        return wxpay;
    }

    /**
     * 微信扫描支付
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Accessors(chain = true)
    public class WXNativePayConfig extends com.github.wxpay.sdk.WXPayConfig {

        /**
         * 微信开发者模式的APP_ID
         */
        private String appId;

        /**
         * 微信支付商户号
         */
        private String mchId;

        /**
         * API密钥
         */
        private String key;

        /**
         * 证书路径
         */
        private String certDataPath;

        /**
         * 证书
         */
        private byte[] certData=null;

        public WXNativePayConfig setCertDataPath(String certDataPath) throws IOException {
            this.certDataPath = certDataPath;
            certData = FileOptionUtil.read(certDataPath);
            return this;
        }

        @Override
        public String getAppID() {
            return appId;
        }

        @Override
        public String getMchID() {
            return mchId;
        }

        @Override
        public String getKey() {
            return key;
        }

        @Override
        public InputStream getCertStream() {
            ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
            return certBis;
        }

        @Override
        public IWXPayDomain getWXPayDomain() {

            IWXPayDomain iwxPayDomain = new IWXPayDomain() {
                @Override
                public void report(String domain, long elapsedTimeMillis, Exception ex) {
                }
                @Override
                public DomainInfo getDomain(com.github.wxpay.sdk.WXPayConfig wxPayConfig) {
                    return new DomainInfo(WXPayConstants.DOMAIN_API, true);
                }
            };

            return iwxPayDomain;
        }

        @Override
        public int getHttpConnectTimeoutMs() {
            return super.getHttpConnectTimeoutMs();
        }

        @Override
        public int getHttpReadTimeoutMs() {
            return super.getHttpReadTimeoutMs();
        }

        @Override
        public boolean shouldAutoReport() {
            return super.shouldAutoReport();
        }

        @Override
        public int getReportWorkerNum() {
            return super.getReportWorkerNum();
        }

        @Override
        public int getReportQueueMaxSize() {
            return super.getReportQueueMaxSize();
        }

        @Override
        public int getReportBatchSize() {
            return super.getReportBatchSize();
        }
    }
}
